/**
 * Created by Administrator on 2016/8/5.
 */
var $ = require('jquery');
var Meta = require('../../chatInfo');

exports.create = function ($parent, tabType, info) {
  var $clueContent = createContent(tabType,info);
  $parent.append($clueContent);
  return new ClueContent($clueContent);
};


function ClueContent() {

}

ClueContent.prototype.update = function (tabType, chatId) {
  var $parent = $('.im-content#chat-' + chatId +' .im-clue-content');
  var $content = $parent.children('.content[data-content="' + tabType +'"]');
  //如果有该tab对应的内容，则展示，否则ajax请求
  if ($content.length) {
    $content.siblings('.content').hide();
    $parent.find('.loading-icon').hide();
    $content.show();
  } else {
    console.info('没该标签内容', tabType);
    $parent.find('.content').hide();
    $parent.find('.loading-icon').show();
    getContentInfo(tabType, null, Meta.chatId, function (err, info) {
      if (err) {
        console.log(err);
      } else {
        $parent.find('.loading-icon').hide();
        var $h = createContent(tabType, info);
        $parent.append($h);
      }

    });
  }
};

function createContent(tabType, info) {
  var $html;
  switch (tabType) {
    case 'inquiry':
      $html = $('<div class="content" data-content="inquiry">' +
        '<p class="inquiry-number">询价单：' + info.inquiryId +
        '</p><p>状态：' + info.status +
        '</p><p>车辆信息：' + info.vin +
        '</p><p>车牌：' + info.plateno +
        '</p><p>车型：' + info.carModel +
        '</p><p>提交时间：' + info.createdTime +
        '</p><a href="/agentBuy/inquiryDetail/' + Meta.clueId +
        '?from=ALL" target="_blank">查看详情&gt;&gt;</a></div>');
      break;
    case 'order':
      $html = $('<div class="content" data-content="order">' +
        '<p class="orderNumber">订单号：' + info.orderId +
        '</p><p>状态：' + info.status +
        '</p><p>店铺：' + info.store +
        '</p><a href="/orders/viewOrderDetail?orderId=' + Meta.clueId +
        '&customerServiceUrl=" target="_blank">查看详情&gt;&gt;</a></div>');
      break;
    case 'product':
      $html = $('<div class="content" data-content="product">' +
        '<p>商品名称：' + info.name +
        '</p><p>品牌：' + info.brand +
        '</p><p>零件号：' + info.partNumber +
        '</p><p>原厂信息：' + info.originalInfo +
        '</p><p>4s店价：' + (info.fsPrice ? '&yen;'+info.fsPrice : '-') +
        '</p><a href="/portal/products?productId=' + Meta.clueId +
        '" target="_blank">查看详情&gt;&gt;</a></div>');
      break;
    case 'store':
      $html = $('<div class="content" data-content="store">' +
        '<p class="storeName">' + info.name +
        '</p><p>供应商地址：' + info.address +
        '</p><p>联系电话： ' + info.telNumber +
        '</p></div>');
      break;
    default:
      $html = $('<p class="no-data">暂无数据</p>');
      break;
  }
  return $html;
}

function makeUrl(tabType, activeChatId) {
  var url;
  if (tabType == 'store') {
    url = '/ec-api/' + tabType + '/' + (activeChatId || Meta.chatId);
    return url;
  }
  url = '/ec-api/' + tabType + '/' + Meta.clueId;
  return url;
}

function getContentInfo(tabType, activeType, activeChatId, callback) {
  var info = null;
  var data = {userId: Meta.username};
  $.ajax({
    method: 'GET',
    url: makeUrl(activeType || tabType, activeChatId),
    data: data,
    success: function (data) {
      info = data;
      callback(null, info);
    },
    error: function (error) {
      console.log(error);
      callback(error);
    }
  });
}
